using System.Collections.Generic;
using UnityEngine;

public class BoxUI : MonoBehaviour
{
	public OpenBoxUI openbox_fun;

	public UIEventListener buyonebtn;

	public UIEventListener buyfivebtn;

	public UIEventListener buytenbtn;

	public UIEventListener openallbtn;

	public UIEventListener helpbtn;

	public UILabel shownumlabel;

	private int[] spendnum = new int[3]
	{
		10,
		45,
		85
	};

	private int[] getboxnum = new int[3]
	{
		1,
		5,
		10
	};

	private int curboxnum;

	private int allopennum;

	public List<BoxData> Box_data;

	public int[] pieceflag;

	public OpenBoxUI openboxui;

	public UIWidget[] fixitem;

	public UILabel[] fixlabel;

	public UIWidget[] pieceitemwi;

	public Piece_item[] pieceitem;

	public TweenPosition[] itemanimas;

	public UISprite boxbg;

	private int allodds;

	private int fixedodds;

	private int[] getfixnum = new int[3];

	private List<BoxData> piecelist = new List<BoxData>();

	private int oddsran;

	private void Start()
	{
		buyonebtn.onClick = buyonefun;
		buyfivebtn.onClick = buyfivefun;
		buytenbtn.onClick = buytenfun;
		openallbtn.onClick = openallfun;
		helpbtn.onClick = helpfun;
	}

	public void enable()
	{
		MenuPlayerprefs.instance.readbox();
		curboxnum = MenuPlayerprefs.instance.haveboxnum;
		allopennum = MenuPlayerprefs.instance.allopenboxnum;
		shownumlabel.text = string.Empty + curboxnum;
		initcalinfo();
		openboxui.SetFalseObj();
	}

	public void buyonefun(GameObject btn)
	{
		if (MenuPlayerprefs.instance.DecDiamond(spendnum[0]))
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
			curboxnum += getboxnum[0];
			MenuPlayerprefs.instance.savebox(getboxnum[0]);
			if (GlobalInf.CurrentQuality < Quality.Low)
			{
				shownumlabel.text = string.Empty + curboxnum;
			}
			MenuUIController.instance.enable_effectui(buyonebtn.transform.position, 3);
			Platform.flurryEvent_box(1);
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			MenuUIController.instance.enable_chargeui(1);
		}
	}

	public void buyfivefun(GameObject btn)
	{
		if (MenuPlayerprefs.instance.DecDiamond(spendnum[1]))
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
			curboxnum += getboxnum[1];
			MenuPlayerprefs.instance.savebox(getboxnum[1]);
			if (GlobalInf.CurrentQuality < Quality.Low)
			{
				shownumlabel.text = string.Empty + curboxnum;
			}
			MenuUIController.instance.enable_effectui(buyfivebtn.transform.position, 3);
			Platform.flurryEvent_box(5);
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			MenuUIController.instance.enable_chargeui(1);
		}
	}

	public void buytenfun(GameObject btn)
	{
		if (MenuPlayerprefs.instance.DecDiamond(spendnum[2]))
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
			curboxnum += getboxnum[2];
			MenuPlayerprefs.instance.savebox(getboxnum[2]);
			if (GlobalInf.CurrentQuality < Quality.Low)
			{
				shownumlabel.text = string.Empty + curboxnum;
			}
			MenuUIController.instance.enable_effectui(buytenbtn.transform.position, 3);
			Platform.flurryEvent_box(10);
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			MenuUIController.instance.enable_chargeui(1);
		}
	}

	public void refershbox()
	{
		shownumlabel.text = string.Empty + curboxnum;
	}

	public void initcalinfo()
	{
		Box_data = MenuDataController.instance.Getboxdata();
		pieceflag = MenuPlayerprefs.instance.pieceflag;
		for (int i = 3; i < Box_data.Count; i++)
		{
			if (pieceflag[i - 3] == 1)
			{
				Box_data[i].id = -1;
			}
		}
		allodds = 0;
		fixedodds = 0;
		for (int j = 0; j < Box_data.Count; j++)
		{
			if (Box_data[j].id != -1)
			{
				allodds += Box_data[j].gailv;
				if (j < 3)
				{
					fixedodds += Box_data[j].gailv;
				}
			}
		}
	}

	public void openallfun(GameObject btn)
	{
		for (int i = 0; i < 3; i++)
		{
			getfixnum[i] = 0;
		}
		piecelist.Clear();
		if (curboxnum <= 0)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
			MenuUIController.instance.enable_hitinfoui(hitinfoid.boxnull);
			return;
		}
		for (int j = 0; j < curboxnum; j++)
		{
			allopennum++;
			if (UnityEngine.Random.Range(0, 10) < 5)
			{
				oddsran = UnityEngine.Random.Range(0, fixedodds);
				for (int k = 0; k < 3; k++)
				{
					oddsran -= Box_data[k].gailv;
					if (oddsran < 0)
					{
						getfixnum[k] += UnityEngine.Random.Range(Box_data[k].minnum, Box_data[k].maxnum + 1);
						break;
					}
				}
			}
			else
			{
				oddsran = UnityEngine.Random.Range(0, fixedodds);
				for (int l = 0; l < 3; l++)
				{
					oddsran -= Box_data[l].gailv;
					if (oddsran < 0)
					{
						getfixnum[l] += UnityEngine.Random.Range(Box_data[l].minnum, Box_data[l].maxnum + 1);
						l = (l + 1) % 3;
						getfixnum[l] += UnityEngine.Random.Range(Box_data[l].minnum, Box_data[l].maxnum + 1);
						break;
					}
				}
			}
			if (allodds == fixedodds)
			{
				break;
			}
			if (allopennum % 10 == 9)
			{
				oddsran = UnityEngine.Random.Range(0, allodds - fixedodds);
				for (int m = 3; m < Box_data.Count; m++)
				{
					if (Box_data[m].id != -1)
					{
						oddsran -= Box_data[m].gailv;
						if (oddsran < 0)
						{
							piecelist.Add(Box_data[m]);
							break;
						}
					}
				}
				continue;
			}
			oddsran = UnityEngine.Random.Range(0, allodds);
			if (oddsran < fixedodds)
			{
				continue;
			}
			oddsran -= fixedodds;
			for (int n = 3; n < Box_data.Count; n++)
			{
				if (Box_data[n].id != -1)
				{
					oddsran -= Box_data[n].gailv;
					if (oddsran < 0)
					{
						piecelist.Add(Box_data[n]);
						break;
					}
				}
			}
		}
		saveGetreward();
		ShowReward();
		openboxui.enable();
	}

	public void saveGetreward()
	{
		if (getfixnum[0] > 0)
		{
			MenuPlayerprefs.instance.AddDiamod(getfixnum[0]);
		}
		if (getfixnum[1] > 0)
		{
			MenuPlayerprefs.instance.AddMoney(getfixnum[1]);
		}
		if (getfixnum[2] > 0)
		{
			MenuPlayerprefs.instance.AddEnergy(getfixnum[2]);
		}
		PieceData pieceData = new PieceData();
		for (int i = 0; i < piecelist.Count; i++)
		{
			pieceData = MenuDataController.instance.Getpiece(piecelist[i].id);
			MenuPlayerprefs.instance.addpiece(pieceData.id, 1);
		}
		MenuPlayerprefs.instance.savebox(-curboxnum);
		curboxnum = 0;
		shownumlabel.text = "0";
		MenuPlayerprefs.instance.savedailytask(4);
	}

	public void ShowReward()
	{
		openbox_fun.clearlist();
		int num = 0;
		if (getfixnum[0] > 0)
		{
			fixitem[0].alpha = 0f; fixitem[0].gameObject.SetActive(false);
			fixlabel[0].text = string.Empty + getfixnum[0];
			fixitem[0].transform.localPosition = new Vector3(-200f + 70f * (float)num, 34f, 0f);
			itemanimas[0].to = new Vector3(-200f + 70f * (float)num, 34f, 0f);
			num++;
			openbox_fun.Additem(0);
		}
		else
		{
			fixitem[0].alpha = 0f; fixitem[0].gameObject.SetActive(false);
		}
		if (getfixnum[1] > 0)
		{
			fixitem[1].alpha = 0f; fixitem[1].gameObject.SetActive(false);
			fixlabel[1].text = string.Empty + getfixnum[1];
			fixitem[1].transform.localPosition = new Vector3(-200f + 70f * (float)num, 34f, 0f);
			itemanimas[1].to = new Vector3(-200f + 70f * (float)num, 34f, 0f);
			num++;
			openbox_fun.Additem(1);
		}
		else
		{
			fixitem[1].alpha = 0f; fixitem[1].gameObject.SetActive(false);
		}
		if (getfixnum[2] > 0)
		{
			fixitem[2].alpha = 0f; fixitem[2].gameObject.SetActive(false);
			fixlabel[2].text = string.Empty + getfixnum[2];
			fixitem[2].transform.localPosition = new Vector3(-200f + 70f * (float)num, 34f, 0f);
			itemanimas[2].to = new Vector3(-200f + 70f * (float)num, 34f, 0f);
			num++;
			openbox_fun.Additem(2);
		}
		else
		{
			fixitem[2].alpha = 0f; fixitem[2].gameObject.SetActive(false);
		}
		int num2 = 0;
		for (int i = 0; i < pieceitem.Length; i++)
		{
			num2 = 0;
			for (int j = 0; j < piecelist.Count; j++)
			{
				if (MenuDataController.instance.piecedata[i].templateid == piecelist[j].id)
				{
					num2++;
				}
			}
			if (num2 > 0)
			{
				pieceitem[i].enable(MenuDataController.instance.piecedata[i], num2);
				if (num < 7)
				{
					pieceitem[i].transform.localPosition = new Vector3(-200f + 70f * (float)num, 34f, 0f);
					itemanimas[i + 3].to = new Vector3(-200f + 70f * (float)num, 34f, 0f);
					openbox_fun.Additem(i + 3);
				}
				else
				{
					pieceitem[i].transform.localPosition = new Vector3(-200f + 70f * (float)(num - 7), -34f, 0f);
					itemanimas[i + 3].to = new Vector3(-200f + 70f * (float)(num - 7), -34f, 0f);
					openbox_fun.Additem(i + 3);
				}
				num++;
				pieceitemwi[i].alpha = 0f; pieceitemwi[i].gameObject.SetActive(false);
			}
			else
			{
				pieceitemwi[i].alpha = 0f; pieceitemwi[i].gameObject.SetActive(false);
			}
		}
		if (num <= 7)
		{
			boxbg.SetDimensions(boxbg.width, 72);
			boxbg.transform.localPosition = new Vector3(0f, 34f, 0f);
		}
		else
		{
			boxbg.SetDimensions(boxbg.width, 144);
			boxbg.transform.localPosition = Vector3.zero;
		}
	}

	public void helpfun(GameObject btn)
	{
		MenuUIController.instance.enable_boxinfoui();
	}
}
